﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Xml;

namespace CodeBetter.Extensions.Tests
{
  [TestFixture]
  public class XmlWriterTests
  {
    [Test]
    public void StartDocument()
    {
      var sb = new StringBuilder();
      using (var w = XmlWriter.Create(sb))
      {
        using (w.StartDocument())
        {
          w.WriteStartElement("test");
          w.WriteEndElement();
        }
      }
      Assert.IsTrue(sb.ToString().IndexOf("<?xml") >= 0);
      Assert.IsTrue(sb.ToString().IndexOf("standalone=\"yes\"") < 0);
    }

    [Test]
    public void StartDocumentStandAlone()
    {
      var sb = new StringBuilder();
      using (var w = XmlWriter.Create(sb))
      {
        using (w.StartDocument(true))
        {
          w.WriteStartElement("test");
          w.WriteEndElement();
        }
      }
      Assert.IsTrue(sb.ToString().IndexOf("<?xml") >= 0);
      Assert.IsTrue(sb.ToString().IndexOf("standalone=\"yes\"") >= 0);
    }

    [Test]
    public void StartElement()
    {
      var sb = new StringBuilder();
      using (var w = XmlWriter.Create(sb))
      {
        using (w.StartDocument())
        {
          using (w.StartElement("test"))
          {
          }
        }
      }
      Assert.IsTrue(sb.ToString().IndexOf("<test />") >= 0);
    }

    [Test]
    public void StartElementFullEnd()
    {
      var sb = new StringBuilder();
      using (var w = XmlWriter.Create(sb))
      {
        using (w.StartDocument())
        {
          using (w.StartElement("test", true))
          {

          }
        }
      }
      Assert.IsTrue(sb.ToString().IndexOf("<test></test>") >= 0);
    }

    [Test]
    public void StartElementNamespace()
    {
      var sb = new StringBuilder();
      using (var w = XmlWriter.Create(sb))
      {
        using (w.StartDocument())
        {
          using (w.StartElement("test", "codebetter"))
          {

          }
        }
      }
      Assert.IsTrue(sb.ToString().IndexOf("xmlns=\"codebetter\"") >= 0);
    }

    [Test]
    public void StartElementNamespaceFullEnd()
    {
      var sb = new StringBuilder();
      using (var w = XmlWriter.Create(sb))
      {
        using (w.StartDocument())
        {
          using (w.StartElement("test", "codebetter", true))
          {

          }
        }
      }
      Assert.IsTrue(sb.ToString().IndexOf("xmlns=\"codebetter\"") >= 0);
      Assert.IsTrue(sb.ToString().IndexOf("</test>") >= 0);
    }

    [Test]
    public void StartElementNamespacePrefix()
    {
      var sb = new StringBuilder();
      using (var w = XmlWriter.Create(sb))
      {
        using (w.StartDocument())
        {
          using (w.StartElement("cb", "test", "codebetter"))
          {

          }
        }
      }
      Assert.IsTrue(sb.ToString().IndexOf("xmlns:cb=\"codebetter\"") >= 0);
    }

    [Test]
    public void StartElementNamespacePrefixFullEnd()
    {
      var sb = new StringBuilder();
      using (var w = XmlWriter.Create(sb))
      {
        using (w.StartDocument())
        {
          using (w.StartElement("cb", "test", "codebetter", true))
          {

          }
        }
      }
      Assert.IsTrue(sb.ToString().IndexOf("xmlns:cb=\"codebetter\"") >= 0);
      Assert.IsTrue(sb.ToString().IndexOf("</cb:test>") >= 0);
    }

    [Test]
    public void StartAttribute()
    {
      var sb = new StringBuilder();
      using (var w = XmlWriter.Create(sb))
      {
        using (w.StartDocument())
        {
          using (w.StartElement("test"))
          {
            using (w.StartAttribute("attr"))
            {
              w.WriteString("123");
            }
          }
        }
      }
      Assert.IsTrue(sb.ToString().IndexOf("attr=\"123\"") >= 0);
    }

    [Test]
    public void StartAttributeNamespace()
    {
      var sb = new StringBuilder();
      using (var w = XmlWriter.Create(sb))
      {
        using (w.StartDocument())
        {
          using (w.StartElement("test"))
          {
            using (w.StartAttribute("attr", "codebetter"))
            {
              w.WriteString("123");
            }
          }
        }
      }
      Assert.IsTrue(sb.ToString().IndexOf("\"codebetter\"") >= 0);
    }

    [Test]
    public void StartAttributeNamespacePrefix()
    {
      var sb = new StringBuilder();
      using (var w = XmlWriter.Create(sb))
      {
        using (w.StartDocument())
        {
          using (w.StartElement("test"))
          {
            using (w.StartAttribute("attr", "codebetter", "cb"))
            {
              w.WriteString("123");
            }
          }
        }
      }
      Assert.IsTrue(sb.ToString().IndexOf("codebetter") >= 0);
      Assert.IsTrue(sb.ToString().IndexOf("\"cb\"") >= 0);
    }
  }
}
